# tmux.conf
#  for tmux 2.1+ : mainly for tmux 2.3 and 2.4 (highly recommended)
#
# @seealso
# - http://manpages.ubuntu.com/manpages/precise/man1/tmux.1.html


# 1. Key Bindings {{{
# ===================

# Key combination prefix is <C-a>, not <C-b>
set-option -g prefix C-a
unbind-key C-b

# use <C-a><C-a> to switch window,
# and <C-a>a to control nested sessions
bind-key C-a last-window
bind-key a send-prefix

# enable vi-mode
setw -g mode-keys vi


# Enable mouse behavior.
# CAVEAT: Mouse configuration changed in tmux 2.1, without backward compatibility, WTF!
# The following configuration works BOTH in tmux 2.1+ and in <2.1.

# [tmux 2.1+]
# By default, entering copy-mode with the scroll wheel does not work.
# Thus, 'tmux-scroll-copy-mode' plugin is enabled (see plugin settings)
# {{{
#set -g mouse-utf8 on
set -g mouse on
# }}}

# [older than tmux 2.1 (legacy)] {{{
#    setw -g mode-mouse on
#    set -g mouse-resize-pane on
#    set -g mouse-select-pane on
#    set -g mouse-select-window on
# }}}


# some useful <C-a> combinations
bind-key : command-prompt
bind-key r source-file ~/.tmux.conf \; display "Reloaded from ~/.tmux.conf"
bind-key L clear-history

bind-key space next-window
bind-key bspace previous-window
bind-key enter next-layout
bind-key C-o rotate-window

# = : main-vertical, Alt = : main-horizontal
bind-key =   select-layout main-vertical
bind-key M-= select-layout main-horizontal
set-window-option -g other-pane-height 25
set-window-option -g other-pane-width 80

# vertical/horizontal split
bind-key v split-window -h -c "#{pane_current_path}"
bind-key s split-window -v -c "#{pane_current_path}"
bind-key | split-window -h -c "#{pane_current_path}"
bind-key _ split-window -v -c "#{pane_current_path}"

# Resize pane (and then enter the resize mode)
# We make the <, >, +, - key repeatable (with shift hold) by using "resize-pane" keytable
bind-key > resize-pane -R 10 \; switch-client -T "resize-pane"
bind-key < resize-pane -L 10 \; switch-client -T "resize-pane"
bind-key -T "resize-pane" > resize-pane -R 5 \; switch-client -T "resize-pane"
bind-key -T "resize-pane" < resize-pane -L 5 \; switch-client -T "resize-pane"

bind-key + resize-pane -D 5  \; switch-client -T "resize-pane"
bind-key - resize-pane -U 5  \; switch-client -T "resize-pane"
bind-key -T "resize-pane" + resize-pane -D \; switch-client -T "resize-pane"
bind-key -T "resize-pane" = resize-pane -D \; switch-client -T "resize-pane"
bind-key -T "resize-pane" - resize-pane -U \; switch-client -T "resize-pane"
bind-key -T "resize-pane" _ resize-pane -U \; switch-client -T "resize-pane"

# resize-pane mode: support h, j, k, l, and arrow keys
bind-key -T "resize-pane" k resize-pane -U \; switch-client -T "resize-pane"
bind-key -T "resize-pane" j resize-pane -D \; switch-client -T "resize-pane"
bind-key -T "resize-pane" h resize-pane -L \; switch-client -T "resize-pane"
bind-key -T "resize-pane" l resize-pane -R \; switch-client -T "resize-pane"

bind-key -T "resize-pane" Up    resize-pane -U \; switch-client -T "resize-pane"
bind-key -T "resize-pane" Down  resize-pane -D \; switch-client -T "resize-pane"
bind-key -T "resize-pane" Left  resize-pane -L \; switch-client -T "resize-pane"
bind-key -T "resize-pane" Right resize-pane -R \; switch-client -T "resize-pane"


# pane/windows navigation
bind-key h select-pane -L
bind-key j select-pane -D
bind-key k select-pane -U
bind-key l select-pane -R
bind-key q display-panes

bind-key C-a last-window
bind-key c new-window
bind-key t next-window
bind-key T previous-window

bind-key 0 select-window -t :0
bind-key 1 select-window -t :1
bind-key 2 select-window -t :2
bind-key 3 select-window -t :3
bind-key 4 select-window -t :4
bind-key 5 select-window -t :5
bind-key 6 select-window -t :6
bind-key 7 select-window -t :7
bind-key 8 select-window -t :8
bind-key 9 select-window -t :9

bind-key , previous-window         # select left
bind-key . next-window             # select right
bind-key Left previous-window      # move left
bind-key Right next-window         # move left

# move windows
bind-key S-Left  swap-window -t :- \; previous-window     # move left
bind-key S-Right swap-window -t :+ \; next-window         # move right

# move windows (for tmux < 3.0); see tmux/tmux#2056
if-shell -b '[ `tmux -V | cut -d" " -f2 | tr -d " |\-|.|[:alpha:]"` -lt 30 ]' \
   'bind-key S-Left  swap-window -t :- ; \
    bind-key S-Right swap-window -t :+ ; \
   '

bind-key / command-prompt "move-window -t '%%'"

# change the number of the current window (i.e. move window)
bind-key % command-prompt -I "#I" "move-window -t '%%'"

# change the name of the current window (shift-a)
bind-key A command-prompt -I "#W" "rename-window '%%'"

# change the title of the current pane
bind-key t command-prompt -p "(rename-pane)" -I "#T" "select-pane -T '%%'"

# synchronize mode (toggle)
# bind-key e set-window-option synchronize-panes

# In pane synchronize mode, make all pane borders yellow and the active one orange.
# NOTE: make sure that the following color configuration is compatible with
# 'pane-border-fg', 'pane-active-border-fg', etc.
bind  e if -F '#{pane_synchronized}' \
                   'setw synchronize-panes off; \
                    setw pane-border-style fg=colour240; \
                    setw pane-active-border-style fg=green,bg=default; \
                   ' \
                   'setw synchronize-panes on; \
                    setw pane-border-style fg=colour226; \
                    setw pane-active-border-style fg=colour202,bg=default; \
                   ' \

# Clunkily connect tmux buffers with the pasteboard.
bind-key Escape copy-mode
bind-key Enter copy-mode
bind-key ] paste-buffer

# vi-mode v: Begin visual-selection in copy mode.
# vi-mode y: Yank the current selection in copy mode.
# tmux version < 2.4
if-shell -b '[ `tmux -V | cut -d" " -f2 | tr -d " |\-|.|[:alpha:]"` -lt 24 ]' \
   'bind-key -t vi-copy "v" begin-selection; \
    bind-key -t vi-copy "y" copy-pipe "pbcopy"; \
   '

# tmux version 2.4+
# see https://github.com/tmux/tmux/issues/754
# see https://github.com/tmux/tmux/issues/592
if-shell -b '[ `tmux -V | cut -d" " -f2 | tr -d " |\-|.|[:alpha:]"` -ge 24 ]' \
   'bind -T copy-mode-vi "v" send-keys -X begin-selection; \
    bind -T copy-mode-vi "y" send-keys -X copy-pipe-and-cancel "pbcopy"; \
   '

bind-key 'p' run-shell "pbpaste | tmux load-buffer - && tmux paste-buffer"

# no delay when switching modes (copy mode, etc.)
set -sg escape-time 0

set-window-option -g display-panes-time 1500

# }}}



# 2. Appereance and General Preferences {{{
# =========================================

# tmux 3.3 or higher: passthrough is turned off by default. turn it on!
if-shell -b '[ `tmux -V | cut -d" " -f2 | tr -d " |\-|.|[:alpha:]"` -ge 33 ]' \
    'set-option -g allow-passthrough on'

# see @tmux-pugins/vim-tmux-focus-events
set -g focus-events on

# Color and Themes
set-option -g default-terminal "xterm-256color"
set-option -as terminal-overrides ",xterm-256color:Tc,xterm-kitty:Tc"     # True-color (24bit)


# let pane/window numbers be 1-indexed
set-option -g base-index 1
set-option -g pane-base-index 1

# Status Bar

# Update status every 1 seconds
set-option -g status-interval 1

# No automatic renaming of windows (from commands on execution)
set-window-option -g automatic-rename off

# Set title, which is also showed at iTerm tab
# For special character sequences such as #S, refer to manual
set-option -g set-titles on
set-option -g set-titles-string '#S #I:#W (#h)' # session name, window index, window title, hostname

# Status Bar
set -g status-justify "left"
set -g status "on"
set -g status-left-style "none"
set -g message-command-style "fg=white,bg=#0087af"
set -g status-right-style "none"
#set -g status-utf8 "on"
set -g status-style "bg=#1c1c1c,none"
set -g message-style "fg=white,bg=#0087af"
set -g pane-border-style "fg=#585858"
set -g status-left-length "100"

# status-left, status-right, window-status-format, window-status-current-format: see the separate script
set -g status-right "#[bg=colour228]Loading ~/.tmux/statusbar.tmux; if it doesn't load, fix your config.#[default]"
run-shell '~/.tmux/statusbar.tmux'

# Reference: http://man7.org/linux/man-pages/man1/tmux.1.html#FORMATS
# Reference: https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg
setw -g window-status-activity-style "none"
setw -g window-status-activity-style "fg=#bcbcbc,bg=#1c1c1c,underscore"
setw -g window-status-separator ""
setw -g window-status-style "fg=#bcbcbc,bg=#1c1c1c,none"

# window modes color (e.g. choosing window: <prefix>w / escape mode)
setw -g mode-style bg=#0087af,fg=white

# pane border colors
set-option -g pane-border-style fg=#585858,bg=default   # inactive pane border: gray
set-option -g pane-active-border-style fg=green,bg=default  # active pane border: green

# Pane number and title shown on the border
set-option -g pane-border-status 'top'
set-option -g pane-border-format '(#{pane_index}) #{?pane_active,#[fg=colour156],}#T#F'

# (DISABLED) Auto hide pane-border-status line when only one pane is displayed (see tmux/tmux#566)
#set-hook -g 'after-new-window'   'run-shell -b "if [ \#{window_panes} -eq 1 ]; then tmux set pane-border-status off; fi"'
#set-hook -g 'after-kill-pane'    'run-shell -b "if [ \#{window_panes} -eq 1 ]; then tmux set pane-border-status off; fi"'
#set-hook -g 'pane-exited'        'run-shell -b "if [ \#{window_panes} -eq 1 ]; then tmux set pane-border-status off; fi"'
#set-hook -g 'after-split-window' 'run-shell -b "if [ \#{window_panes} -gt 1 ]; then tmux set pane-border-status top; fi"'

# window notifications
set-window-option -g monitor-activity on
set-option -g visual-activity off          # no 'Activity in window <n>'

# Allow the arrow key to be used immediately after changing windows
set-option -g repeat-time 0

# More history and scrollback
set-option -g history-limit 100000

# }}}


# 3. vim-tmux-navigator {{{
# =========================

# Smart pane switching with awareness of vim splits
is_vim='echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?)(diff)?$"'
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "send-keys C-l"
bind -n 'C-\' if-shell "$is_vim" "send-keys 'C-\\'" "select-pane -l"


# 3. Tmux Plugins {{{
# ===================

# List of plugins
# @see https://github.com/tmux-plugins/tpm
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
#set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'nhdaly/tmux-scroll-copy-mode'
#set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'laktak/extrakto'
set -g @plugin 'jaclu/tmux-menus'


# tmux-copycat
# use vim-like navigation (n, N)
set -g @copycat_next 'N'
set -g @copycat_prev 'n'

# extrakto
set -g @extrakto_split_size "12"


# Enable plugins
run-shell '~/.tmux/plugins/tpm/tpm'

# }}}
